package com.lyj.linearalgebra.user.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.lyj.linearalgebra.comment.domain.Comment;
import com.lyj.linearalgebra.user.domain.User;

import cn.itcast.jdbc.TxQueryRunner;

public class UserDao {
	
	private QueryRunner qr = new TxQueryRunner();

	public User findByUserName(String username) {
		
		try {
			String sql = "select * from user where username=?";
			return qr.query(sql, new BeanHandler<User>(User.class),username);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public User findByEmail(String email) {
		
		try {
			String sql = "select * from user where email=?";
			return qr.query(sql, new BeanHandler<User>(User.class),email);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public void add(User user) {
		
		try {
			String sql = "insert into user value(?,?,?,?,?)";
			Object[] params = {user.getUid(),user.getUsername(),
				user.getPassword(),user.getEmail(),user.getCode()};
			qr.update(sql, params);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}


}
